home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / function.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  17.1 KB  |  486 lines

  1. #ifndef __STD_FUNCTIONAL__
  2. #define __STD_FUNCTIONAL__
  3. #pragma option push -b -a4 -Vx- -Ve- -w-inl -w-aus -w-sig
  4.  
  5. /***************************************************************************
  6.  *
  7.  * functional - global template functions
  8.  *
  9.  * $Id: functional,v 1.34 1996/08/28 18:41:39 smithey Exp $
  10.  *
  11.  ***************************************************************************
  12.  *
  13.  * Copyright (c) 1994
  14.  * Hewlett-Packard Company
  15.  *
  16.  * Permission to use, copy, modify, distribute and sell this software
  17.  * and its documentation for any purpose is hereby granted without fee,
  18.  * provided that the above copyright notice appear in all copies and
  19.  * that both that copyright notice and this permission notice appear
  20.  * in supporting documentation.  Hewlett-Packard Company makes no
  21.  * representations about the suitability of this software for any
  22.  * purpose.  It is provided "as is" without express or implied warranty.
  23.  *
  24.  *
  25.  ***************************************************************************
  26.  *
  27.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  28.  * ALL RIGHTS RESERVED *
  29.  * The software and information contained herein are proprietary to, and
  30.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  31.  * intends to preserve as trade secrets such software and information.
  32.  * This software is furnished pursuant to a written license agreement and
  33.  * may be used, copied, transmitted, and stored only in accordance with
  34.  * the terms of such license and with the inclusion of the above copyright
  35.  * notice.  This software and information or any other copies thereof may
  36.  * not be provided or otherwise made available to any other person.
  37.  *
  38.  * Notwithstanding any other lease or license that may pertain to, or
  39.  * accompany the delivery of, this computer software and information, the
  40.  * rights of the Government regarding its use, reproduction and disclosure
  41.  * are as set forth in Section 52.227-19 of the FARS Computer
  42.  * Software-Restricted Rights clause.
  43.  * 
  44.  * Use, duplication, or disclosure by the Government is subject to
  45.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  46.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  47.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  48.  * P.O. Box 2328, Corvallis, Oregon 97339.
  49.  *
  50.  * This computer software and information is distributed with "restricted
  51.  * rights."  Use, duplication or disclosure is subject to restrictions as
  52.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  53.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  54.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  55.  * then the "Alternate III" clause applies.
  56.  *
  57.  **************************************************************************/
  58.  
  59. #include <stdcomp.h>
  60.  
  61. #ifndef _RWSTD_NO_NAMESPACE
  62. namespace std {
  63. #endif
  64.  
  65. //
  66. // The bases of many of the function objects here.
  67. //
  68.  
  69. template <class Arg, class Result>
  70. struct unary_function
  71. {
  72.     typedef Arg argument_type;
  73.     typedef Result result_type;
  74. };
  75.  
  76. template <class Arg1, class Arg2, class Result>
  77. struct binary_function
  78. {
  79.     typedef Arg1 first_argument_type;
  80.     typedef Arg2 second_argument_type;
  81.     typedef Result result_type;
  82. };
  83.  
  84. //
  85. // Arithmetic operators.
  86. //
  87.  
  88. template <class T>
  89. struct plus : public binary_function<T, T, T>
  90. {
  91.     typedef _TYPENAME binary_function<T, T, T>::second_argument_type second_argument_type;
  92.     typedef _TYPENAME binary_function<T, T, T>::first_argument_type first_argument_type;
  93.     typedef _TYPENAME binary_function<T, T, T>::result_type result_type;
  94.     T operator() (const T& x, const T& y) const { return x + y; }
  95. };
  96.  
  97. template <class T>
  98. struct minus : public binary_function<T, T, T>
  99. {
  100.     typedef _TYPENAME binary_function<T, T, T>::second_argument_type second_argument_type;
  101.     typedef _TYPENAME binary_function<T, T, T>::first_argument_type first_argument_type;
  102.     typedef _TYPENAME binary_function<T, T, T>::result_type result_type;
  103.     T operator() (const T& x, const T& y) const { return x - y; }
  104. };
  105.  
  106. template <class T>
  107. struct multiplies : public binary_function<T, T, T>
  108. {
  109.     typedef _TYPENAME binary_function<T, T, T>::second_argument_type second_argument_type;
  110.     typedef _TYPENAME binary_function<T, T, T>::first_argument_type first_argument_type;
  111.     typedef _TYPENAME binary_function<T, T, T>::result_type result_type;
  112.     T operator() (const T& x, const T& y) const { return x * y; }
  113. };
  114.  
  115. template <class T>
  116. struct divides : public binary_function<T, T, T>
  117. {
  118.     typedef _TYPENAME binary_function<T, T, T>::second_argument_type second_argument_type;
  119.     typedef _TYPENAME binary_function<T, T, T>::first_argument_type first_argument_type;
  120.     typedef _TYPENAME binary_function<T, T, T>::result_type result_type;
  121.     T operator() (const T& x, const T& y) const { return x / y; }
  122. };
  123.  
  124. template <class T>
  125. struct modulus : public binary_function<T, T, T>
  126. {
  127.     typedef _TYPENAME binary_function<T, T, T>::second_argument_type second_argument_type;
  128.     typedef _TYPENAME binary_function<T, T, T>::first_argument_type first_argument_type;
  129.     typedef _TYPENAME binary_function<T, T, T>::result_type result_type;
  130.     T operator() (const T& x, const T& y) const { return x % y; }
  131. };
  132.  
  133. template <class T>
  134. struct negate : public unary_function<T, T>
  135. {
  136.     typedef _TYPENAME unary_function<T,T>::argument_type argument_type;
  137.     typedef _TYPENAME unary_function<T,T>::result_type result_type;
  138.     T operator() (const T& x) const { return -x; }
  139. };
  140.  
  141. //
  142. // Comparisons.
  143. //
  144.  
  145. template <class T>
  146. struct equal_to : public binary_function<T, T, bool>
  147. {
  148.     typedef _TYPENAME binary_function<T, T, bool>::second_argument_type second_argument_type;
  149.     typedef _TYPENAME binary_function<T, T, bool>::first_argument_type first_argument_type;
  150.     typedef _TYPENAME binary_function<T, T, bool>::result_type result_type;
  151.     bool operator() (const T& x, const T& y) const { return x == y; }
  152. };
  153.  
  154. template <class T>
  155. struct not_equal_to : public binary_function<T, T, bool>
  156. {
  157.     typedef _TYPENAME binary_function<T, T, bool>::second_argument_type second_argument_type;
  158.     typedef _TYPENAME binary_function<T, T, bool>::first_argument_type first_argument_type;
  159.     typedef _TYPENAME binary_function<T, T, bool>::result_type result_type;
  160.     bool operator() (const T& x, const T& y) const { return x != y; }
  161. };
  162.  
  163. template <class T>
  164. struct greater : public binary_function<T, T, bool>
  165. {
  166.     typedef _TYPENAME binary_function<T, T, bool>::second_argument_type second_argument_type;
  167.     typedef _TYPENAME binary_function<T, T, bool>::first_argument_type first_argument_type;
  168.     typedef _TYPENAME binary_function<T, T, bool>::result_type result_type;
  169.     bool operator() (const T& x, const T& y) const { return x > y; }
  170. };
  171.  
  172. template <class T>
  173. struct less : public binary_function<T, T, bool>
  174. {
  175.     typedef _TYPENAME binary_function<T, T, bool>::second_argument_type second_argument_type;
  176.     typedef _TYPENAME binary_function<T, T, bool>::first_argument_type first_argument_type;
  177.     typedef _TYPENAME binary_function<T, T, bool>::result_type result_type;
  178.     bool operator() (const T& x, const T& y) const { return x < y; }
  179. };
  180.  
  181. template <class T>
  182. struct greater_equal : public binary_function<T, T, bool>
  183. {
  184.     typedef _TYPENAME binary_function<T, T, bool>::second_argument_type second_argument_type;
  185.     typedef _TYPENAME binary_function<T, T, bool>::first_argument_type first_argument_type;
  186.     typedef _TYPENAME binary_function<T, T, bool>::result_type result_type;
  187.     bool operator() (const T& x, const T& y) const { return x >= y; }
  188. };
  189.  
  190. template <class T>
  191. struct less_equal : public binary_function<T, T, bool>
  192. {
  193.     typedef _TYPENAME binary_function<T, T, bool>::second_argument_type second_argument_type;
  194.     typedef _TYPENAME binary_function<T, T, bool>::first_argument_type first_argument_type;
  195.     typedef _TYPENAME binary_function<T, T, bool>::result_type result_type;
  196.     bool operator() (const T& x, const T& y) const { return x <= y; }
  197. };
  198.  
  199. //
  200. // Logical operations.
  201. //
  202.  
  203. template <class T>
  204. struct logical_and : public binary_function<T, T, bool>
  205. {
  206.     typedef _TYPENAME binary_function<T, T, bool>::second_argument_type second_argument_type;
  207.     typedef _TYPENAME binary_function<T, T, bool>::first_argument_type first_argument_type;
  208.     typedef _TYPENAME binary_function<T, T, bool>::result_type result_type;
  209.     bool operator() (const T& x, const T& y) const { return x && y; }
  210. };
  211.  
  212. template <class T>
  213. struct logical_or : public binary_function<T, T, bool>
  214. {
  215.     typedef _TYPENAME binary_function<T, T, bool>::second_argument_type second_argument_type;
  216.     typedef _TYPENAME binary_function<T, T, bool>::first_argument_type first_argument_type;
  217.     typedef _TYPENAME binary_function<T, T, bool>::result_type result_type;
  218.     bool operator() (const T& x, const T& y) const { return x || y; }
  219. };
  220.  
  221. template <class T>
  222. struct logical_not : public unary_function<T, bool>
  223. {
  224.     typedef _TYPENAME unary_function<T,bool>::argument_type argument_type;
  225.     typedef _TYPENAME unary_function<T,bool>::result_type result_type;
  226.     bool operator() (const T& x) const { return !x; }
  227. };
  228.  
  229. //
  230. // Negators.
  231. //
  232.  
  233. template <class Predicate>
  234. class unary_negate : public unary_function<_TYPENAME Predicate::argument_type,
  235.                                            bool>
  236. {
  237.   protected:
  238.     Predicate pred;
  239.   public:
  240.     typedef _TYPENAME unary_function<_TYPENAME Predicate::argument_type,bool>::argument_type argument_type;
  241.     typedef _TYPENAME unary_function<_TYPENAME Predicate::argument_type,bool>::result_type result_type;
  242.     _EXPLICIT unary_negate (const Predicate& x) : pred(x) {}
  243.     bool operator() (const _TYPENAME unary_function<
  244.               _TYPENAME Predicate::argument_type,bool>::argument_type& x) const
  245.                     { return !pred(x); }
  246. };
  247.  
  248. template <class Predicate>
  249. inline unary_negate<Predicate> not1(const Predicate& pred)
  250. {
  251.     return unary_negate<Predicate>(pred);
  252. }
  253.  
  254. template <class Predicate> 
  255. class binary_negate
  256.     : public binary_function<_TYPENAME Predicate::first_argument_type,
  257.                              _TYPENAME Predicate::second_argument_type, bool>
  258. {
  259.   protected:
  260.     Predicate pred;
  261.   public:
  262.     typedef _TYPENAME binary_function<_TYPENAME Predicate::first_argument_type,
  263.        _TYPENAME Predicate::second_argument_type, bool>::second_argument_type second_argument_type;
  264.     typedef _TYPENAME binary_function<_TYPENAME Predicate::first_argument_type,
  265.        _TYPENAME Predicate::second_argument_type, bool>::first_argument_type first_argument_type;
  266.     typedef _TYPENAME binary_function<_TYPENAME Predicate::first_argument_type,
  267.        _TYPENAME Predicate::second_argument_type, bool>::result_type result_type;
  268.     _EXPLICIT binary_negate (const Predicate& x) : pred(x) {}
  269.     bool operator() (const _TYPENAME binary_function<_TYPENAME Predicate::first_argument_type,
  270.                  _TYPENAME Predicate::second_argument_type, bool>::first_argument_type& x, 
  271.                  const _TYPENAME binary_function<_TYPENAME Predicate::first_argument_type,
  272.                  _TYPENAME Predicate::second_argument_type, bool>::second_argument_type& y) const
  273.     {
  274.         return !pred(x, y); 
  275.     }
  276. };
  277.  
  278. template <class Predicate>
  279. inline binary_negate<Predicate> not2(const Predicate& pred)
  280. {
  281.     return binary_negate<Predicate>(pred);
  282. }
  283.  
  284. //
  285. // Binders.
  286. //
  287.  
  288. template <class Operation> 
  289. class binder1st :public unary_function<_TYPENAME Operation::second_argument_type,
  290.                                        _TYPENAME Operation::result_type>
  291. {
  292.   protected:
  293.     Operation op;
  294.     _TYPENAME Operation::first_argument_type value;
  295.   public:
  296.     typedef _TYPENAME unary_function<_TYPENAME Operation::second_argument_type,
  297.                   _TYPENAME Operation::result_type>::argument_type argument_type;
  298.     typedef _TYPENAME unary_function<_TYPENAME Operation::second_argument_type,
  299.                   _TYPENAME Operation::result_type>::result_type result_type;
  300.     binder1st (const Operation& x,
  301.                const _TYPENAME Operation::first_argument_type& y)
  302.         : op(x), value(y) {}
  303.     _TYPENAME unary_function<_TYPENAME Operation::second_argument_type,
  304.              _TYPENAME Operation::result_type>::result_type
  305.              operator() (const _TYPENAME unary_function<_TYPENAME Operation::second_argument_type,
  306.                 _TYPENAME Operation::result_type>::argument_type& x) const
  307.     {
  308.         return op(value, x); 
  309.     }
  310. };
  311.  
  312. template <class Operation, class T>
  313. inline binder1st<Operation> bind1st (const Operation& op, const T& x)
  314. {
  315.    typedef _TYPENAME Operation::first_argument_type the_argument_type;
  316.    return binder1st<Operation>(op, the_argument_type(x));
  317. }
  318.  
  319. template <class Operation> 
  320. class binder2nd : public unary_function<_TYPENAME Operation::first_argument_type,
  321.                                         _TYPENAME Operation::result_type>
  322. {
  323.   protected:
  324.     Operation op;
  325.     _TYPENAME Operation::second_argument_type value;
  326.   public:
  327.     typedef _TYPENAME unary_function<_TYPENAME Operation::first_argument_type,
  328.                   _TYPENAME Operation::result_type>::argument_type argument_type;
  329.     typedef _TYPENAME unary_function<_TYPENAME Operation::first_argument_type,
  330.                   _TYPENAME Operation::result_type>::result_type result_type;
  331.     binder2nd (const Operation& x,
  332.                const _TYPENAME Operation::second_argument_type& y) 
  333.         : op(x), value(y) {}
  334.     _TYPENAME unary_function<_TYPENAME Operation::first_argument_type,
  335.              _TYPENAME Operation::result_type>::result_type
  336.              operator() (const _TYPENAME unary_function<_TYPENAME Operation::first_argument_type,
  337.                 _TYPENAME Operation::result_type>::argument_type& x) const
  338.     {
  339.         return op(x, value); 
  340.     }
  341. };
  342.  
  343. template <class Operation, class T>
  344. inline binder2nd<Operation> bind2nd (const Operation& op, const T& x)
  345. {
  346.   typedef _TYPENAME Operation::second_argument_type the_argument_type;
  347.   return binder2nd<Operation>(op, the_argument_type(x));
  348. }
  349.  
  350.  
  351. //
  352. // Adaptors.
  353. //
  354.  
  355. template <class Arg, class Result>
  356. class pointer_to_unary_function : public unary_function<Arg, Result>
  357. {
  358.   protected:
  359.     Result (*ptr)(Arg);
  360.   public:
  361.     typedef _TYPENAME unary_function<Arg,Result>::argument_type argument_type;
  362.     typedef _TYPENAME unary_function<Arg,Result>::result_type result_type;
  363.     _EXPLICIT pointer_to_unary_function (Result (*x)(Arg)) : ptr(x) {}
  364.     Result operator() (Arg x) const { return ptr(x); }
  365. };
  366.  
  367. template <class Arg, class Result>
  368. inline pointer_to_unary_function<Arg, Result> ptr_fun(Result (*x)(Arg))
  369. {
  370.     return pointer_to_unary_function<Arg, Result>(x);
  371. }
  372.  
  373. template <class Arg1, class Arg2, class Result>
  374. class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>
  375. {
  376.   protected:
  377.     Result (*ptr)(Arg1, Arg2);
  378.   public:
  379.     typedef _TYPENAME binary_function<Arg1, Arg2, Result>::second_argument_type second_argument_type;
  380.     typedef _TYPENAME binary_function<Arg1, Arg2, Result>::first_argument_type first_argument_type;
  381.     typedef _TYPENAME binary_function<Arg1, Arg2, Result>::result_type result_type;
  382.     _EXPLICIT pointer_to_binary_function (Result (*x)(Arg1, Arg2)) : ptr(x) {}
  383.     Result operator() (Arg1 x, Arg2 y) const
  384.     {
  385.         return ptr(x, y); 
  386.     }
  387. };
  388.  
  389. template <class Arg1, class Arg2, class Result>
  390. inline pointer_to_binary_function<Arg1, Arg2, Result> 
  391. ptr_fun(Result (*x)(Arg1, Arg2))
  392. {
  393.     return pointer_to_binary_function<Arg1, Arg2, Result>(x);
  394. }
  395.  
  396. //
  397. // Pointer to member function adaptors
  398. //
  399. // mem_fun_t, mem_fun1_t
  400. //
  401.  
  402. template <class S, class T> 
  403. class mem_fun_t  : public unary_function<T*,S>
  404. {
  405.   S (T::*pmf)();
  406.  
  407.   public:
  408.     _EXPLICIT mem_fun_t(S (T::*p)()) : pmf(p)
  409.     { ; }
  410.     S operator()(T* p)
  411.     { return (p->*pmf)(); }
  412. };
  413.  
  414.  
  415. template <class S, class T, class A> 
  416. class mem_fun1_t : public binary_function<T*,A,S>
  417. {
  418.   S (T::*pmf)(A);
  419.  
  420.   public:
  421.     _EXPLICIT mem_fun1_t(S (T::*p)(A)) : pmf(p)
  422.     { ; }
  423.     S operator()(T* p, A a)
  424.     { return (p->*pmf)(a); }
  425. };
  426.  
  427. template <class S, class T> 
  428. inline mem_fun_t<S,T> mem_fun(S (T::*f)())
  429. {
  430.   return mem_fun_t<S,T>(f);
  431. }
  432.  
  433. template <class S, class T, class A> 
  434. inline mem_fun1_t<S,T,A> mem_fun1(S (T::*f)(A))
  435. {
  436.   return mem_fun1_t<S,T,A>(f);
  437. }
  438.  
  439. //
  440. // mem_fun_ref_t, mem_fun1_ref_t
  441. //
  442.  
  443. template <class S, class T> 
  444. class mem_fun_ref_t  : public unary_function<T,S>
  445. {
  446.   S (T::*pmf)();
  447.  
  448.   public:
  449.     _EXPLICIT mem_fun_ref_t(S (T::*p)()) : pmf(p)
  450.     { ; }
  451.     S operator()(T& p)
  452.     { return (p.*pmf)(); }
  453. };
  454.  
  455.  
  456. template <class S, class T, class A> 
  457. class mem_fun1_ref_t : public binary_function<T,A,S>
  458. {
  459.   S (T::*pmf)(A);
  460.  
  461.   public:
  462.     _EXPLICIT mem_fun1_ref_t(S (T::*p)(A)) : pmf(p) 
  463.     { ; }
  464.     S operator()(T& p, A a)
  465.     { return (p.*pmf)(a); }
  466. };
  467.  
  468. template <class S, class T> 
  469. inline mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)())
  470. {
  471.   return mem_fun_ref_t<S,T>(f);
  472. }
  473.  
  474. template <class S, class T, class A> 
  475. inline mem_fun1_ref_t<S,T,A> mem_fun1_ref(S (T::*f)(A))
  476. {
  477.   return mem_fun1_ref_t<S,T,A>(f);
  478. }
  479.  
  480. #ifndef _RWSTD_NO_NAMESPACE
  481. }
  482. #endif
  483.  
  484. #pragma option pop
  485. #endif /*__STD_FUNCTIONAL__*/
  486.